<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Recharge extends Model
{
    protected $table = 'recharges';

    protected $fillable = [
        'id',
        'uid', //用户id
        'bid', //商家id
        'order_id', //订单id
        'num', //数量
        'price', //成交价
        'total_price', //总额
        'status', //状态 1 未付款 2 已付款 3 已完成 4 已取消 5 申诉中 6申述完结
        'mark_no',//参考号
        'type', //1买入 2卖出
        'order_no', //订单号
        'pay_time', //付款时间
        'alipay_indent', //支付宝订单号
        'backup_status', //备份订单状态
    ];

    const S_ONE = 1;
    const S_TWO = 2;
    const S_THREE = 3;
    const S_FOUR = 4;
    const S_FIVE = 5;
    const S_SIX = 6;
    public static $STATUS = [
        self::S_ONE => '未付款',
        self::S_TWO => '已付款',
        self::S_THREE => '已完成',
        self::S_FOUR => '已取消',
        self::S_FIVE => '申诉中',
        self::S_SIX => '申诉完结',
    ];

    const TYPE_ONE = 1;
    const TYPE_TWO = 2;
    public static $TYPE = [
        self::TYPE_ONE => '买入',
        self::TYPE_TWO => '卖出',
    ];

    public function genre($genre = NULL,$id){
        if($genre == 1) {
            $where['status'] = 1;
        } elseif ($genre == 2) {
            $where['status'] = 2;
            $where['type'] = 1;
        } elseif ($genre == 3) {
            $where['status'] = 2;
            $where['type'] = 2;
        }
        $where['uid'] = $id;
        return $where;
    }
    //用户
    public function buyer()
    {
        return $this->hasOne('App\\Models\\User', 'id', 'uid');
    }
    //商家
    public function seller()
    {
        return $this->hasOne('App\\Models\\User', 'id', 'bid');
    }
    //商家挂单记录
    public function rech_order()
    {
        return $this->hasOne('App\\Models\\RechOrder', 'id', 'order_id');
    }
    //商家信息
    public function enters()
    {
        return $this->hasOne('App\\Models\\Enter', 'uid', 'bid');
    }
    //关联申诉
    public function appeal_order()
    {
        return $this->hasOne('App\\Models\\Appeal', 'rid', 'id')->orderBy('created_at', 'desc');
        //return $this->appeals()->first();
    }

    //采用一堆多关联
    public function appeals()
    {
        return $this->hasMany('App\\Models\\Appeal', 'rid', 'id')->orderBy('created_at', 'desc');
    }
}
